package com.onepiece.shipelves.common.myutils.linked;

class Node{
    int value;
    Node next;
}

//head是一个不存储数据的节点
public class LinkList {


    public static Node createLink(int[] x){
        Node head;
        Node tail = new Node();
        head = tail;
        Node node;
        for (int i = 0; i < x.length; i++) {
            node = new Node();
            node.value = x[i];
            tail.next = node;
            tail = node;
        }
        return head;
    }

    public static void display(Node head){
        Node node = head.next;
        while (node != null){
            System.out.println(node.value);
            node = node.next;
        }
    }

    //一次循环交换两个节点，temp需要第一时间去标记下一个节点
    public static void reverse(Node head){
        Node p = head.next;
        Node q = p.next;
        p.next = null;//不能忘记，否则会出现环
        Node temp;
        while(q != null){
            temp = q.next;
            q.next = p;
            p = q;
            q = temp;
        }
        if(q == null){
            head.next = p;
        }
    }

    public static void main(String[] args) {
        Node link = createLink(new int[]{1, 3, 5, 6, 7});
        display(link);
        reverse(link);
        display(link);

    }
}
